package com.bw.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson.JSON;
import com.bw.entity.OrderInfo;
import com.bw.mapper.OrderInfoMapper;
import com.bw.service.OrderInfoService;
import com.fasterxml.jackson.annotation.JsonAlias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    OrderInfoMapper  orderInfoMapper;

    @Transactional
    @Override
    public OrderInfo add(OrderInfo orderInfo) {
        //用户账号、来源、支付方式非空校验
        Integer userId = orderInfo.getUserId();

        Assert.notNull(userId,"用户账号不能为空");
        Assert.notNull(orderInfo.getSourceId(),"来源不能为空");
        Assert.notBlank(orderInfo.getPayType(),"支付方式不能为空");

        //订单编号自动生成，不能重复
        String orderNum = UUID.randomUUID().toString();
        orderInfo.setOrderNum(orderNum);

        //订单状态，默认为：“未支付
        if (orderInfo.getStatus() == null){
            orderInfo.setStatus(0);
        }
        //打印方法名称和参数到控制台
        System.out.println("方法的名称 add()");
        System.out.println("方法的参数  " + JSON.toJSONString(orderInfo));

        //订单入库
        orderInfoMapper.add(orderInfo);
        return orderInfo;
    }

    @Override
    public List<OrderInfo> selectByParam(OrderInfo orderInfo) {



        return orderInfoMapper.selectByParam(orderInfo);
    }

    public static void main(String[] args) {
        // 生成uUid,
        String string = UUID.randomUUID().toString();
        System.out.println(string);
        String string2 = UUID.randomUUID().toString();
        System.out.println(string2);
        String string3 = UUID.randomUUID().toString();
        System.out.println(string3);
    }
}
